package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.myapplication.domain.getQidItem;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.concurrent.TimeUnit;

public class testHttpActivity extends AppCompatActivity implements View.OnClickListener {
    private String respStr;
    private OkHttpClient okHttpClient;
    private TextView tw_qid;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_http);
        Button btn_send = findViewById(R.id.btn_send);
        tw_qid = findViewById(R.id.tw_qid);
        btn_send.setOnClickListener(this);
        okHttpClient = new OkHttpClient();
    }

    @Override
    public void onClick(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String url = "http://175.178.156.135:1690/Sn/GetSnClassAndTag?sn=CRQ3A2900357";
                    url="http://175.178.156.135:1690/Sn/getUserInfo?account=CRQ3A2900317";

                    Request request = new Request.Builder().url(url).build();
                    Call call = okHttpClient.newCall(request);
                    Response response = call.execute();    //同步请求,阻塞线程
                    respStr = response.body().string();
                    Gson gson = new Gson();
                    getQidItem qidItem = gson.fromJson(respStr, getQidItem.class);
                    String qid = qidItem.getData().getQid();
                    tw_qid.setText(qid);
                } catch (Exception e) {
                    Log.d("解析失败", e.toString());
                    e.printStackTrace();
                }
            }
        }).start();

    }

    //okhttp异步get请求
    public void syncGet(View view) {
        String url = "http://175.178.156.135:1690/Sn/GetSnClassAndTag?sn=CRQ3A2900357";

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10000, TimeUnit.MILLISECONDS)
                .build();
        //创建请求内容
        final Request request = new Request.Builder()
                .get()
                .url(url)
                .build();
        //用client去创建请求任务
        Call task = okHttpClient.newCall(request);
        //异步请求
        task.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.d("请求失败", e.toString());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                int code = response.code();
                if (code == HttpURLConnection.HTTP_OK) {
                    ResponseBody body = response.body();
                    System.out.println(body.string());
                }
            }
        });

    }

}

